package com.fml.ft_study.service;

import com.fml.ft_study.dao.BillDAO;
import com.fml.ft_study.domain.Bill;
import com.fml.ft_study.domain.menu;
import org.junit.Test;

import java.util.List;
import java.util.UUID;

public class BillService {
    private BillDAO billDAO=new BillDAO();
    private  MenuService menuService=new MenuService();
    private DiningTableService diningTableService =new DiningTableService();

    public  boolean updataBill(int menuId,int nums,int diningTableId){
       String BillId= UUID.randomUUID().toString();
        menu menu = menuService.MenuById(menuId);
        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')",BillId,menuId,nums,menu.getPrice()*nums,diningTableId);
     if (update<=0)  return false ;
        boolean st = diningTableService.updataDiningTable(diningTableId, "就餐中");
        return st;
    }

//    listBill()显示所有订单
    public List<Bill>listBills(){
        List<Bill> bills = billDAO.queryMulti("SELECT * FROM bill", Bill.class);
       return bills;
    }
    //根据餐桌号判断是否存在未付款的账单
    public  boolean NostateById(int DiningTable){

        Bill bill = billDAO.querySingle("SELECT   *FROM  bill  WHERE  diningTableId=?  and state='未结账'LIMIT 0,1", Bill.class, DiningTable);
        if (bill!=null){
            return true;
        }
        return false;
    }
    //根据餐桌号来更新所有账单的状态，并重置相应餐桌的状态
    public  boolean PayBill(int diningTableId,String modepay){
        int update = billDAO.update("UPDATE bill SET state = ? WHERE state = '未结账'and diningTableId=? ", modepay, diningTableId);
        if (update<=0){
            return false;
        }
        boolean b = diningTableService.setIinitDiningTable(diningTableId);
        if (b){
            return true;

        }
     return  false;
    }


}
